package com.example.rocketmqpublisher.controller;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wy
 * @Description TODO
 * @Date 2024/2/26 17:03
 * @Version 1.0
 */
@RestController
public class SendMessageController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping(value = "/send/msg")
    public String sendMsg(String msg){

        //String destination 目标地, Message<?> message
        SendResult mailTopic = this.rocketMQTemplate.syncSend("mailTopic", msg);
        return mailTopic.toString();

    }

    @GetMapping(value = "/send/tag/msg")
    public String sendTagMsg(String msg,String tag){
        // 目的地destination – formats: `topicName话题:tags标识'
        //类似于rabbitmq中的direct路由交换机   tags路似routerKey
        String destination="mailTopic";
        if(tag!=null){
            destination+=":"+tag;
        }
        //String destination 目标地, Message<?> message
        SendResult mailTopic = this.rocketMQTemplate.syncSend(destination, msg);
        return mailTopic.toString();

    }

}
